﻿using Abp.Application.Services;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.ObjectMapping;
using CPPProject.CppModel;
using CPPProject.CppServices.Dto;
using Microsoft.EntityFrameworkCore;
using OplugAbpProject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace CPPProject.CppServices
{
    public class T_Biz_ContractAppService : AsyncCrudAppService<T_Biz_Contract, T_Biz_ContractDto, Guid, PagedResultRequestDto, UpdateT_Biz_ContractDto, UpdateT_Biz_ContractDto>, IT_Biz_ContractAppService
    {
        private readonly IObjectMapper _objectMapper;
        private readonly IT_Biz_CustomerAPPService _CustomerAPPService;
        public T_Biz_ContractAppService(IRepository<T_Biz_Contract, Guid> repository, IObjectMapper objectMapper, IT_Biz_CustomerAPPService biz_CustomerAPPService) : base(repository)
        {
            _objectMapper = objectMapper;
            _CustomerAPPService = biz_CustomerAPPService;
        }
        #region 合同管理
        /// <summary>
        /// 合同管理
        /// </summary>
        /// <param name="nCompanyID">公司ID</param>
        /// <param name="skipCount"></param>
        /// <param name="maxResultCount"></param>
        /// <param name="cCode">合同编号</param>
        /// <param name="iBizType">业务类型（0期租1次租2贸易）</param>
        /// <param name="dStartDate">开始时间</param>
        /// <param name="dEndDate">结束时间</param>
        /// <param name="cPayments">结算方式（月结，季度结算，年度结算）</param>
        /// <param name="iStatus">状态（0未执行1执行中2已关闭）</param>
        /// <param name="cCusCode">客户ID</param>
        /// <returns></returns>
        public async Task<PagedResultDto<T_Biz_ContractDto>> GetList(string nCompanyID, int skipCount = 0, int maxResultCount = 10, string cCode = "", string iBizType = "", string dStartDate = "", string dEndDate = "", string cPayments = "", string iStatus = "", string cCusCode = "")
        {
            PagedResultDto<T_Biz_ContractDto> resultDto = new PagedResultDto<T_Biz_ContractDto>();
            List<T_Biz_Contract> lists = new List<T_Biz_Contract>();
            Expression<Func<T_Biz_Contract, bool>> expr = o => o.NCompanyID == nCompanyID;
            if (!string.IsNullOrEmpty(cCode))//合同编号
            {
                expr = expr.ExpressionAnd(o => o.CCode == cCode);
            }
            if (!string.IsNullOrEmpty(iBizType))///业务类型（0期租1次租2贸易）
            {
                expr = expr.ExpressionAnd(o => o.IBizType == iBizType);
            }
            if (!string.IsNullOrEmpty(dStartDate))//开始时间
            {
                expr = expr.ExpressionAnd(o => o.DStartDate.ToString() == dStartDate);
            }
            if (!string.IsNullOrEmpty(dEndDate))//结束时间
            {
                expr = expr.ExpressionAnd(o => o.DEndDate.ToString() == dEndDate);
            }
            if (!string.IsNullOrEmpty(cPayments))//结算方式（月结，季度结算，年度结算）
            {
                expr = expr.ExpressionAnd(o => o.CPayments == cPayments);
            }
            if (!string.IsNullOrEmpty(iStatus))//状态（0未执行1执行中2已关闭）
            {
                expr = expr.ExpressionAnd(o => o.IStatus == iStatus);
            }
            if (!string.IsNullOrEmpty(cCusCode))//客户ID
            {
                var list = await _CustomerAPPService.GetList2(cCusCode);
                expr = expr.ExpressionAnd(o=>o.CCusCode==list.CCusCode);
            }
            lists = await Repository.GetAll().Where(expr).ToListAsync();
            var dtos = lists.Skip(skipCount).Take(maxResultCount).ToList();
            resultDto.Items = _objectMapper.Map<List<T_Biz_ContractDto>>(dtos);
            resultDto.TotalCount = lists.Count;
            return resultDto;
        }
        #endregion 
    }
}
